// ITI 1120 Winter 2012, Lab 5, Example 1
// Name: Gilbert Arbez, Student# 1234567

import java.io.*;

/**
 * This program finds the average of values in an array.
 */

class Lab5Ex1
{
    public static void main (String[] args)
    {

        // DECLARE VARIABLES/DATA DICTIONARY
        // INTERMEDIATES
        double[] arrValues; // Array of values for which to find average
        int n;              // Length of array x
        double average;     // Average of all values in x
        
        // PRINT OUT IDENTIFICATION INFORMATION     
        System.out.println();
        System.out.println("ITI 1120 Winter 2012, Lab 5, Example 1");
        System.out.println("Name: Gilbert Arbez, Student# 1234567");
        System.out.println();

        // READ IN GIVENS
        System.out.println( "Please enter several real values on a line." );
        arrValues = ITI1120.readDoubleLine();
        n = arrValues.length;
        
        // CALL the problem solving method
       average = calculateAverage(arrValues,n);
        
        // PRINT OUT RESULTS AND MODIFIEDS
        System.out.println("The average is " + average );
    }//end main

    /* 
     * Method:  calculateAverage
     * Description: Calculates the average of the values in the
     *              referenced array.
     * Givens: arr - reference to an array of real numbers.
     *         n - number of elements in the array
     */
    public static double calculateAverage(double [] arr, int n)
    {
       // Results
        double average;     // RESULT:        Average of all values in x

       // Intermediates
        double sum;         // INTERMEDIATE:  Running total of array values 
        int index;          // INTERMEDIATE:  Current array position
       
       // Body
        sum = 0.0;
        index = 0;
        
        while ( index < n )
        {
            sum = sum + arr[index];
            index = index + 1;
        }
        
        average = sum/n;

       // return the results
       return(average);
    }
     
} //end class
